package zero.tech.games.mj.service;

import java.util.List;

import org.springframework.stereotype.Service;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.Resp;
import zero.tech.games.common.Code;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.data.consts.LOGICCMD;
import zero.tech.games.logic.service.api.ILogicService;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.mj.data.MjGameBase;
import zero.tech.games.mj.data.MjRoomStatus;
import zero.tech.games.mj.data.MjTimer;
import zero.tech.games.mj.data.consts.GameConst;
import zero.tech.games.mj.data.consts.MjConst;
import zero.tech.games.mj.data.entity.PlayerData;
import zero.tech.games.mj.service.api.IMjService;
import zero.tech.games.mj.service.api.IRoomTimerService;
import zero.tech.games.mj.util.CardUtil;
import zero.tech.games.mj.util.GameLogic;
import zero.tech.wsnet.controller.LogicController;
import zero.tech.wsnet.data.Message;

/**
 * 房间线程处理类
 * @author Administrator
 *
 */
@Service
public class RoomTimerServiceImpl implements IRoomTimerService{

	/**
	 * 倒计时完毕处理逻辑
	 */
	@Override
	public void roomTimer(Room room) {
		int room_status=room.getRoom_status();
		if(room_status==MjRoomStatus.DEFAULT) {
			checkGameStart(room);
		}else if(room_status==MjRoomStatus.DA_PAI) {
			autoOperateDaPai(room);
		}if(room_status==MjRoomStatus.WAIT_NEXT_ROUND){
			checkGameStart(room);
		}else if(room_status==MjRoomStatus.CALCULATING) {
			
		}
	}

	/**
	 * 检查是否可以开始游戏
	 * @param room
	 */
	private void checkGameStart(Room room) {
		IMjService gameService = ZeroContext.getInstance(IMjService.class);
		MjTimer mjTimer = (MjTimer) room.getTimer();
		int room_status = room.getRoom_status();
		if(room_status == RoomStatus.DEFAULT) {
			//检查所有人是否都准备了
			List<LogicController> birdPeopers=LogicUtil.birdPeopers(room);
			if(birdPeopers.size() == room.getMax_player() && room.getAllControllers().size() == room.getMax_player()) {
				gameService.gameStart(room);
			}
		}else {
			//检查所有人是否都准备了
			List<LogicController> readyPeopers=LogicUtil.readyPeopers(room);
			if(readyPeopers.size() == room.getMax_player() && room.getAllControllers().size() == room.getMax_player()) {
				gameService.gameStart(room);
			}else {
				if(room_status == RoomStatus.WAIT_NEXT_ROUND) {
					ILogicService logicService = ZeroContext.getInstance(ILogicService.class);
					//自动准备
					long now = System.currentTimeMillis();
					long lastBeginToReadyTime = mjTimer.lastBeginToReadyTime;
					List<LogicController> notReadyPeopers = LogicUtil.notReadyPeopers(room);
					if(now - lastBeginToReadyTime >= GameConst.RESULT_WAIT_TIME) {
						for(LogicController notReadyPeople : notReadyPeopers) {
							logicService.reqReady(room,notReadyPeople);
						}
					}
				}
			}
		}
	}
	
	/**
	 * 自动操作打牌
	 * @param room
	 */
	public static void autoOperateDaPai(Room room) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		if(gameBase.getMjConfig().getAuto_deposit() > 0) {
			PlayerData operData = gameBase.getOperData();
			//是否在托管列表里面,在就执行托管逻辑
			if(gameBase.getAuto_deposit().containsKey(operData.getUid())) {
				depositOper(room, gameBase);
			}else {
				MjTimer mjTimer = (MjTimer) room.getTimer();
				long now=System.currentTimeMillis();
				long lastTimer = mjTimer.getLastTimer();
				if(now - lastTimer >= gameBase.getMjConfig().getAuto_deposit() * 1000) {
					//长时间不打牌,自动托管
					gameBase.getAuto_deposit().put(operData.getUid(), now);
					//广播某人进入托管
					LogicUtil.broadRoomAllControllersInclude(room, Message.build(LOGICCMD.NOTIFY_ENTER_AUTODEPOSIT, Resp.build(Code.OK, "进入托管",PckUtil.packageNotifyEnterAutoDeposit(operData.getUid(),now))));
				}
			}
		}
	}
	
	/**
	 * 托管操作
	 * @param room
	 * @param gameBase
	 */
	private static void depositOper(Room room, MjGameBase gameBase) {
		MjTimer mjTimer = (MjTimer) room.getTimer();
		IMjService gameService = ZeroContext.getInstance(IMjService.class);
		long now = System.currentTimeMillis();
		long lastTimer = mjTimer.getLastTimer();
		//自动胡牌过牌
		if(now - lastTimer >= GameConst.DA_PAI_TIME) {
			for (LogicController controller : room.getAllControllers().values()) {
				PlayerData playerVo = gameBase.getPlayerUid(controller.getId());
				if (playerVo == null) {
					continue;
				}
				if (playerVo.getActionArr().isEmpty()) {
					// 没有可以操作的
					continue;
				}else {
					int type = 0;
					//默认请求 过
					String info = "";
					type = MjConst.PASS;
					//有胡就胡
					if (GameLogic.isContainCtrl(playerVo.getActionArr(), MjConst.HU)) {
						type = MjConst.HU;
					}
					if (GameLogic.isContainCtrl(playerVo.getActionArr(), MjConst.HU_ZM)) {
						type = MjConst.HU_ZM;
					}
					gameService.reqCtrlCard(room, controller, info, type);
				}
			}
		}
		lastTimer = mjTimer.getLastTimer();
		if(now - lastTimer >= GameConst.DA_PAI_TIME) {
			//自动打牌
			PlayerData playerVo = gameBase.getOperData();
			LogicController userByChair = playerVo.getController(room);
			if (playerVo.isOut()) {
				//托管出牌,目前默认出定缺牌==>最后摸起来的牌
				if (!playerVo.isWin()) {
					int outCard = -1;
					if (outCard == -1) {
						outCard = playerVo.getCurrCard();
						List<Integer> handCard = playerVo.getHandCard();
						boolean isHave = false;
						for (Integer integer : handCard) {
							if (outCard == integer) {
								isHave = true;
								break;
							}
						}
						if (!isHave) {
							outCard = handCard.get(0);
						}
						
					}
					if (CardUtil.isGuiCard(outCard, room)) {
						List<Integer> handCard = playerVo.getHandCard();
						for (int i = 0; i < handCard.size(); i++) {
							if (!CardUtil.isGuiCard(handCard.get(i), room)) {
								outCard = handCard.get(i);
								break;
							}
						}
						
					}
					if (userByChair != null) {
						gameService.reqOutCard(room, userByChair, outCard);
					}
				}
			}
		}
	}

}
